/*
 * Copyright 2011-2016 YueJi.com All right reserved. This software is the confidential and proprietary information of
 * YueJi.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YueJi.com.
 */
package com.bops.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bops.biz.UserTools;
import com.bops.biz.permission.Role;
import com.bops.biz.permission.RoleConfig;
import com.bops.biz.permission.RoleGroup;
import com.bops.cache.UserCache;
import com.bops.dal.user.dao.RoleDao;
import com.bops.dal.user.dataobject.RoleDO;
import com.bops.dal.user.queryobject.RoleQuery;

/**
 * 标签的service层，提供对外接口
 * 
 * @author pgj 2011-9-8 下午2:45:07
 */
public class RoleService {

    private static final Logger logger        = LoggerFactory.getLogger(UserTools.class);

    private RoleDao roleDao;
    private List<Role>    allRoleList;
    private static Object lockObject = new Object();

    private RoleConfig          roleConfig;
    
    public RoleConfig getRoleConfig() {
        return roleConfig;
    }

    
    public void setRoleConfig(RoleConfig roleConfig) {
        this.roleConfig = roleConfig;
    }

    public RoleDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }
    
    /**
     * 批量更新用户权限
     * @param addList
     * @param deleteList
     */
    public void batchUpdateRole(final List<RoleDO> addList,final List<RoleDO> deleteList)
    {
        roleDao.batchUpdateRole(addList, deleteList);
        UserCache.cleanUserRoleMap();
    }
    
    /**
     * 返回需要的权限
     * @param query
     * @return
     */
    public List<RoleDO> listRole(RoleQuery query)
    {
        return roleDao.listRole(query);
    }
    
    /**
     * 返回Spring定义的所有的Role
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<Role> getAllRoleList() {
        if (allRoleList == null) {
            synchronized (lockObject) {
                if (allRoleList == null) {
                    allRoleList = new ArrayList<Role>();
                    Map map = CommonServiceLocator.getAllRole();
                    if (map != null) {
                        Iterator iterator = map.keySet().iterator();
                        while (iterator.hasNext()) {
                            Object key = iterator.next();
                            if (key != null) {
                                Object value = map.get(key);
                                if (value instanceof Role) {
                                    Role tmpRole = (Role) value;
                                    if (tmpRole.isEnabled()) {
                                        tmpRole.setId((String) key);
                                        allRoleList.add(tmpRole);
                                    }
                                }
                            }
                        }
                    }
                }
                if (allRoleList!=null)
                {
                    for (Role r:allRoleList)
                    {
                        try
                        {
                            r.check(r.getId());
                        }
                        catch (Exception e)
                        {
                            logger.error("-------------------------JavaVM Exit!");
                            logger.error(e.getMessage(),e);
                            System.exit(-1);
                        }
                    }
                }
            }
        }
        return allRoleList;
    }
    
    /**
     * 得到权限分类名
     * 
     * @return the permissionGroup
     */
    public List<String> getPermissionGroup() {
        return roleConfig.getGroupNames();
    }

    /**
     * 得到权限组
     * 
     * @return
     */
    public List<RoleGroup> getRoleGroups() {
        return roleConfig.getRoleGroups();
    }

    /**
     * 根据组名获取权限组
     * 
     * @param roleGroupName
     * @return
     */
    public RoleGroup getRoleGroup(String roleGroupName) {
        if (StringUtils.isEmpty(roleGroupName)) {
            return null;
        }
        List<RoleGroup> roleGroups = getRoleGroups();
        if (roleGroups == null || roleGroups.size() == 0) {
            return null;
        }
        for (RoleGroup roleGroup : roleGroups) {
            if (roleGroupName.equals(roleGroup.getName())) {
                return roleGroup;
            }
        }
        return null;
    }
}
